﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LuckyLoot.ServiceFacade.Contracts;
using LuckyLoot.Domain.Entities.People;
using LuckyLoot.Common.Validation;
using LuckyLoot.Domain.ValueObjects;
using LuckyLoot.Common.Repositories;
using LuckyLoot.Common.Contexts;

namespace LuckyLoot.ServiceFacade.Implementation
{
    public class CustomerManagementService : ICustomerManagementService
    {
        public Customer CreateCustomer(string name, string phone, string email, string facebookPage)
        {
            ValidState.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(name));

            FacebookPage fbPage = facebookPage;

            Customer customer = new Customer();
            customer.Name = name;
            customer.Phone = phone;
            customer.Email = email;
            customer.Facebook = fbPage;

            using (Repository<Customer, ApplicationContext> repository = new Repository<Customer, ApplicationContext>())
            {
                repository.Add(customer);
                repository.Save();
            }

            return customer;
        }

        public Customer UpdateCustomer(int customerID, string name, string phone, string email, string facebookPage)
        {
            ValidState.Requires<ArgumentNullException>(customerID > 0);
            ValidState.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(name));
            ValidState.Requires<ArgumentNullException>(!string.IsNullOrWhiteSpace(phone) || !string.IsNullOrWhiteSpace(email));

            Customer customer;
            using (Repository<Customer, ApplicationContext> repository = new Repository<Customer, ApplicationContext>())
            {
                customer = this.GetCustomer(customerID);
                
                FacebookPage fbPage = facebookPage;

                customer.Name = name;
                customer.Phone = phone;
                customer.Email = email;
                customer.Facebook = fbPage;

                repository.Edit(customer);
                repository.Save();
            }

            return customer;
        }

        public Customer GetCustomer(int customerID)
        {
            ValidState.Requires<ArgumentNullException>(customerID > 0);

            Customer customer = null;
            using (Repository<Customer, ApplicationContext> repository = new Repository<Customer, ApplicationContext>())
            {
                customer = repository.GetById(customerID);
                ValidState.Requires<InvalidOperationException>(customer != null, string.Format("Não foi possível encontrar o cliente de id {0}.", customerID));
            }

            return customer;
        }

        public List<Customer> GetAllCustomers()
        {
            var customers = new List<Customer>();
            using (Repository<Customer, ApplicationContext> repository = new Repository<Customer, ApplicationContext>())
            {
                customers = repository.GetAll().ToList<Customer>();
            }

            return customers;
        }
    }
}
